package com.orangelife.service.propFeeReductionDetail.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeReductionDetail.IPropFeeReductionDetailService;
import com.orangelife.service.propFeeReductionDetail.model.PropFeeReductionDetailDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeReductionDetail类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 13:59:04
 * ==============================================================
 */
@Service("propFeeReductionDetailService")
public class PropFeeReductionDetailServiceImpl extends
		MybaitsGenericDaoSubLogic implements IPropFeeReductionDetailService {
	private static Logger logger = Logger
			.getLogger(PropFeeReductionDetailServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeReductionDetailServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeReductionDetail(
			PropFeeReductionDetailDto propFeeReductionDetail)
			throws AppBizException {

		Assert.notNull(propFeeReductionDetail);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeReductionDetail.getStartDate())) {
			if (propFeeReductionDetail.getStartDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "折减开始月份长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReductionDetail.getEndDate())) {
			if (propFeeReductionDetail.getEndDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "折减结束月份长度超出");
			}
		}

		propFeeReductionDetail.setDetailID(IdUtil.getuuId());
		propFeeReductionDetail.setItemStat(TypeConst.FLAG_ON);
		propFeeReductionDetail.setTscreate(new Date());
		propFeeReductionDetail.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeReductionDetailMapper.insertSelective",
				propFeeReductionDetail);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeReductionDetail(
			PropFeeReductionDetailDto propFeeReductionDetail)
			throws AppBizException {

		Assert.notNull(propFeeReductionDetail);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeReductionDetail.getDetailID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeReductionDetailDto oldPropFeeReductionDetail = this
				.findPropFeeReductionDetailByID(propFeeReductionDetail
						.getDetailID());

		if (!StringUtils.isBlank(propFeeReductionDetail.getStartDate())) {
			if (propFeeReductionDetail.getStartDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "折减开始月份长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReductionDetail.getEndDate())) {
			if (propFeeReductionDetail.getEndDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "折减结束月份长度超出");
			}
		}

		if (oldPropFeeReductionDetail == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeReductionDetail.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeReductionDetailMapper.updateByPrimaryKeySelective",
				propFeeReductionDetail);
	}

	@Override
	public PropFeeReductionDetailDto findPropFeeReductionDetailByID(
			String propFeeReductionDetailID) throws AppBizException {
		if (StringUtils.isBlank(propFeeReductionDetailID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeReductionDetailDto oldPropFeeReductionDetail = this
				.getSqlSession()
				.selectOne(
						"PropFeeReductionDetailSearchMapper.selectByPrimaryKey",
						propFeeReductionDetailID);
		if (oldPropFeeReductionDetail == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeReductionDetail.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeReductionDetail;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeReductionDetailByLogic(
			String propFeeReductionDetailID) throws AppBizException {

		if (StringUtils.isBlank(propFeeReductionDetailID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeReductionDetailDto oldPropFeeReductionDetail = this
				.findPropFeeReductionDetailByID(propFeeReductionDetailID);

		oldPropFeeReductionDetail.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeReductionDetail.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeReductionDetailMapper.updateByPrimaryKeySelective",
				oldPropFeeReductionDetail);
	}

	@Override
	public List<Map<String, Object>> findPropFeeReductionDetailByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReductionDetailSearchMapper.findAllPropFeeReductionDetail",
						map);
		return list;
	}

	@Override
	public List<PropFeeReductionDetailDto> getPropFeeReductionDetailByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeReductionDetailDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReductionDetailSearchMapper.getAllPropFeeReductionDetailDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeReductionDetailByRelevantID(
			String detailID, String reductionID, String feeTypeID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("detailID", detailID);
		map.put("reductionID", reductionID);
		map.put("feeTypeID", feeTypeID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReductionDetailSearchMapper.selectPropFeeReductionDetailDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeReductionDetailDto> findPropFeeReductionDetailByRelevantID(
			String detailID, String reductionID, String feeTypeID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("detailID", detailID);
		map.put("reductionID", reductionID);
		map.put("feeTypeID", feeTypeID);

		List<PropFeeReductionDetailDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReductionDetailSearchMapper.findPropFeeReductionDetailDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeReductionDetailDto> bauthfindPropFeeReductionDetail(
			List<String> propFeeReductionDetailIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeReductionDetailDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReductionDetailSearchMapper.batchFindPropFeeReductionDetail",
						propFeeReductionDetailIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeReductionDetailByMap(
			Map<String, Object> propFeeReductionDetail) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReductionDetailSearchMapper.batchFindPropFeeReductionDetailByMap",
						propFeeReductionDetail);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	public List<Map<String, Object>> findReDetail(String reductionID,
			String feeTypeID) throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("reductionID", reductionID);
		map.put("feeTypeID", feeTypeID);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeReductionDetailSearchMapper.findReDetail", map);
		return list;
	}

}